package com.jxtti.service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.jxtti.dao.AdminMenuDao;
import com.jxtti.entity.Admin_Menu;

@Service
@Transactional(rollbackFor = { RuntimeException.class, Exception.class })
public class Admin_Menu_Service {
	private final static Logger log= Logger.getLogger(Admin_Menu_Service.class);	

	@Autowired
    private AdminMenuDao adminMenuDao;
	
	
	public List<Admin_Menu>  queryAll(Map<String, Object> data)
	{
		return  adminMenuDao.queryByList(data);
	}	
	
	
	public List<Admin_Menu>  findByPid(Map<String, Object> data)
	{
		return  adminMenuDao.findByPid(data);
	}	
	
	/**
	 * 返回easyui数结构的组织架构
	 * 
	 * @param data
	 * @return
	 */
	public List<Map<String, Object>> getEasyUiTree(Map<String, String> data) {
		List<Map<String, Object>> result = new ArrayList<Map<String, Object>>();
		List<Admin_Menu> list=new ArrayList<Admin_Menu>(); 
		
		if (null == data.get("parentID")) {
			data.put("parentID", "0");
		}	
		
		list.addAll(adminMenuDao.getTreeNodes(data));
		
		if (null != list && 0 != list.size()) { //判断是否有数据
			for (int i = 0; i < list.size(); i++) { //循环数据
				
				Map<String, Object> node = new HashMap<String, Object>();					
				node.put("id", list.get(i).getId()); //是 getCode	
				node.put("parentID", list.get(i).getParentID());	
				node.put("name", list.get(i).getName());					
				node.put("url", list.get(i).getUrl());
				node.put("sortID", list.get(i).getSortID());
				node.put("icon", list.get(i).getIcon());
				node.put("levelID", list.get(i).getLevelID());
				node.put("visible", list.get(i).getVisible());		
				if (data.get("roleId")!=null) {
					if(list.get(i).getIsHasMenu().equals("true")){
						node.put("checked", true);	 //是否选中
					}					
				}				
//				if ("true".equals(list.get(i).getIsParent())) { //是父级节点
//					node.put("state","closed");	 //异步加载	 		
//				}else{
//					node.put("state","open");		
//				}				
				
				/** 自定义属性
				    Map<String, Object> attrNode = new HashMap<String, Object>();							
					attrNode.put("code", list.get(i).getCode());				
					node.put("attributes",attrNode);
				 */				
				if ("true".equals(list.get(i).getIsParent())) { //是父级节点
					// 区域节点，需要展开下级
					List<Map<String, Object>> childResult = new ArrayList<Map<String, Object>>();
					
					Map<String, String> childData=data; //传递 type 和status
					System.out.println(list.get(i).getId());
					childData.put("parentID", list.get(i).getId().toString());//参数 父类 改变了	
					
					
					childResult=getEasyUiTree(childData);//---------------递归 			
		
					node.put("children", childResult);
				}				
				result.add(node);
			}
		}
		return result;
	}
	
	
	public int add(Admin_Menu tCityAQIWrite)
	{
		int insertCount = adminMenuDao.add(tCityAQIWrite);
		return insertCount;
	}	

	
	public int update(Admin_Menu tCityAQIWrite)
	{
		int updateCount = adminMenuDao.update(tCityAQIWrite);
		return updateCount;
	}	

	
	public int delete(Admin_Menu entity)
	{
		int deleteCount = adminMenuDao.delete(entity);		
		return deleteCount;
	}
	
	
	public Admin_Menu queryAdminMenuById(Admin_Menu entity)
	{
		entity = adminMenuDao.queryAdminMenuById(entity);
		return entity;
	}

}
